package com.xzzz.common.nacos;

import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.cloud.nacos.discovery.NacosDiscoveryAutoConfiguration;
import com.alibaba.cloud.nacos.discovery.NacosDiscoveryClientConfiguration;
import com.alibaba.cloud.nacos.registry.NacosServiceRegistryAutoConfiguration;
import com.sun.management.OperatingSystemMXBean;
import com.xzzz.common.nacos.controller.DiscoveryController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.context.annotation.Bean;

import java.lang.management.ManagementFactory;

/**
 * @author xzzz
 */
@AutoConfigureAfter(NacosDiscoveryAutoConfiguration.class)
@AutoConfigureBefore(NacosDiscoveryClientConfiguration.class)
public class NacosConfig {

    private static final Logger log = LoggerFactory.getLogger(NacosServiceRegistryAutoConfiguration.class);

    public NacosConfig(NacosDiscoveryProperties properties) {
        log.warn("6.1 设置 Nacos Custom Metadata");

        OperatingSystemMXBean operatingSystemMxBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        // CPU 核数
        properties.getMetadata().put("CPU-CORE", String.valueOf(operatingSystemMxBean.getAvailableProcessors()));
        // 内存数
        properties.getMetadata().put("MEMORY", String.valueOf(operatingSystemMxBean.getTotalPhysicalMemorySize() / 1024));
    }

    @Bean
    public DiscoveryController discoveryController() {
        return new DiscoveryController();
    }
}
